package hlhj.fhp.supreme.customview

import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.util.AttributeSet
import android.util.Log
import android.view.View
import android.view.animation.LinearInterpolator

class MarqueeTextView3 : View {
    lateinit var mPaint: Paint
    var mleft: Float = 0f
    var mAnimation: ValueAnimator? = null
    var mHeight: Int = 0
    var mWidth: Int = 0
    var mContent: String? = null
        set(value) {
            field = value
            if (mContent!=null&&mContent!!.length!!>0) {
                Log.e("fhpp","let?")
                var rect = Rect()
                mPaint.getTextBounds(mContent, 0, mContent!!.length, rect)
                mHeight = rect.bottom - rect.top
                mWidth = rect.right - rect.left
                if (mWidth > measuredWidth) { //字小于控件宽度就不滚，如果摇滚动就去掉判断
                    startAnimation()

                } else {
//                    endAnimation()
                    startAnimation()

                }
            } else {
                mWidth = 0
                mHeight = 0
                endAnimation()
            }
            invalidate()
        }

    init {
        mPaint = Paint()
        mPaint.isAntiAlias = true
        mPaint.textSize = 22f
        mPaint.color = Color.BLACK
        mPaint.style = Paint.Style.STROKE
    }

    constructor(context: Context) : super(context)
    constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr)

    fun startAnimation() {
        if (mAnimation!=null){
            endAnimation()
        }
        if(mAnimation==null){
            mAnimation= ObjectAnimator.ofFloat(0f,mWidth.toFloat()*3/2)
//            mAnimation= ObjectAnimator.ofFloat(0f,0f)
        }
        Log.e("fhpp","外面来了")
        mAnimation?.let {
            it.repeatCount=-1
            it.interpolator=LinearInterpolator()
            it.duration=(mContent?.length?:0)* 120L//120 滚动比率
            it.repeatMode=ValueAnimator.RESTART
            Log.e("fhpp","打印一下看看开始没有")
            it.start()
            it.addUpdateListener {
                var animatedValue = it.getAnimatedValue()
                mleft = animatedValue as Float
                invalidate()
            }
        }
    }

    fun endAnimation() {
        mAnimation?.let {
            it.cancel()
            mAnimation = null
        }
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        var widthMode = MeasureSpec.getMode(widthMeasureSpec)
        var heightMode = MeasureSpec.getMode(heightMeasureSpec)

        var mearsureWidth = MeasureSpec.getSize(widthMeasureSpec)
        var measureHeight = MeasureSpec.getSize(heightMeasureSpec)
        var width:Int=0
        var height:Int=0
        var rect = Rect()
        mContent?.let {
            mPaint.getTextBounds(it, 0, it.length, rect)
            mHeight = rect.bottom - rect.top
            mWidth = rect.right - rect.left
        }

        if (widthMode==MeasureSpec.AT_MOST){
            width= Math.min(mearsureWidth,mWidth)
        }else{
            width=mearsureWidth
        }
        if (heightMode==MeasureSpec.AT_MOST){
            height= Math.min(measureHeight,mHeight)
        }else{
            height=measureHeight
        }
        setMeasuredDimension(width,height)

    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        if (mContent!=null&&mContent!!.length!!>0) {
            canvas?.save()
            canvas?.clipRect(0,0,measuredWidth,measuredHeight)
            canvas?.drawText(mContent, mWidth/2-mleft.toFloat(), (measuredHeight / 2 +mHeight / 2).toFloat(), mPaint)

            canvas?.restore()
        }

    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        endAnimation()
    }

}